package com.carmeth.sudoqu;

import java.util.Arrays;
//import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
//import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;

public class PuzzleGridView extends Activity implements OnClickListener {
	
	private enum TypeButton{NORMAL,WARNING};

	private final String CLASS = this.getClass().getSimpleName();
	int requestCode;
	private int[][] easyPuzzle = {{ 3, 6, 0, 0, 0, 0, 0, 0, 0 },
								  { 0, 0, 4, 2, 3, 0, 8, 0, 0 },
								  { 0, 0, 0, 0, 0, 4, 2, 0, 0 },
								  { 0, 7, 0, 4, 6, 0, 0, 0, 3 },
								  { 8, 2, 0, 0, 0, 0, 0, 1, 4 },
								  { 5, 0, 0, 0, 1, 3, 0, 2, 0 },
								  { 0, 0, 1, 9, 0, 0, 0, 0, 0 },
								  { 0, 0, 7, 0, 4, 8, 3, 0, 0 },
								  { 0, 0, 0, 0, 0, 0, 0, 4, 5 } };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_sudoqu);

		//generatePuzzle(1);

		TableLayout tl = (TableLayout) findViewById(R.id.sudoqu_grid);
		for (int i = 0; i < 9; i++) {
			TableRow r = new TableRow(getApplicationContext());
			for (int j = 0; j < 9; j++) {

				ImageButton b = createNumImg(easyPuzzle[i][j]);
				int id = Integer.parseInt((i + 1) + "" + (j + 1));

				if (easyPuzzle[i][j] == 0) {
					//b.setText("");
					b.setOnClickListener(this);
					b.setId(id);
				} else {
					//b.setText(Integer.toString(easyPuzzle[i][j]));
					b.setId(id);
				}
				r.addView(b, j);
			}
			tl.addView(r, i);
		}
	}

	@Override
	public void onClick(View v) {
		Log.v(CLASS, "Button Id:" + v.getId());
		Intent i = new Intent(getApplicationContext(), SelectNumber.class);
		i.putExtra("button", v.getId());
		startActivityForResult(i, requestCode);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		int value = Integer.parseInt(data.getExtras().getString("value"));
		int id = data.getExtras().getInt("button");
		String valueS = data.getExtras().getString("value");

		Log.v(CLASS, "button: " + id);
		Log.v(CLASS, "value: " + valueS);

		ImageButton b = (ImageButton) findViewById(id);

		int i = id / 10 - 1;
		int j = id % 10 - 1;

		setWarningSelection(i, j, value, b);

		easyPuzzle[i][j] = value;

		for (int k = 0; k < 9; k++) {
			String f = "";
			for (int l = 0; l < 9; l++) {
				f = f + easyPuzzle[k][l] + ":";
			}
			Log.v(CLASS, f);
		}
	}

	public void setWarningSelection(int i, int j, int value, ImageButton b) {

		boolean fila = isRepeated(getHorizontal(i, easyPuzzle), value);
		boolean columna = isRepeated(getVertical(j, easyPuzzle), value);
		boolean quadrant = isRepeated(getQuadrant(i, j, easyPuzzle), value);

		Log.v(CLASS, "fila: " + fila + " columna: " + columna + " quadrant: "
				+ quadrant);

		if (fila || columna || quadrant) 
			setNumImg(value, b, TypeButton.WARNING);
		else
			setNumImg(value, b, TypeButton.NORMAL);
	}

	private int[] getHorizontal(int i, int[][] puzzle) {
		int[] aux = new int[9];

		for (int j = 0; j < aux.length; j++)
			aux[i] = puzzle[i][j];

		return aux;
	}

	private int[] getVertical(int j, int[][] puzzle) {
		int[] aux = new int[9];

		for (int i = 0; i < aux.length; i++)
			aux[i] = puzzle[i][j];

		return aux;
	}

	private int[] getQuadrant(int i, int j, int[][] puzzle) {
		int[] aux = new int[9];

		int indAux = 0;
		int fila = 0;
		int columna = 0;
		int filaLim = 0;
		int columnaLim = 0;

		switch (i) {
		case 0:
		case 1:
		case 2:
			fila = 0;
			break;
		case 3:
		case 4:
		case 5:
			fila = 3;
			break;
		case 6:
		case 7:
		case 8:
			fila = 6;
			break;
		}
		filaLim = fila + 2;

		switch (j) {
		case 0:
		case 1:
		case 2:
			columna = 0;
			break;
		case 3:
		case 4:
		case 5:
			columna = 3;
			break;
		case 6:
		case 7:
		case 8:
			columna = 6;
			break;
		}
		columnaLim = columna + 2;

		for (i = fila; i <= filaLim; i++)
			for (j = columna; j <= columnaLim; j++)
				aux[indAux++] = puzzle[i][j];

		return aux;
	}

	private boolean isRepeated(int[] rcq, int value) {
		Arrays.sort(rcq);

		int repeat = Arrays.binarySearch(rcq, value);

		return repeat > -1;
	}

//	private int[][] generatePuzzle(int levelDifficulty) {
//		Random r = new Random();
//		int[][] puzzle = new int[9][9];
//		int numGen = 0;
//
//		for (int i = 0; i < 9; i++) {
//			for (int j = 0; j < 9; j++) {
//			}
//		}
//
//		return puzzle;
//	}

	private ImageButton createNumImg(int num) {
		ImageButton numImg = new ImageButton(getApplicationContext());

		switch (num) {
		case 0:
			numImg.setImageResource(R.drawable.cero_btn);
			break;
		case 1:
			numImg.setImageResource(R.drawable.uno_btn);
			break;
		case 2:
			numImg.setImageResource(R.drawable.dos_btn);
			break;
		case 3:
			numImg.setImageResource(R.drawable.tres_btn);
			break;
		case 4:
			numImg.setImageResource(R.drawable.cuatro_btn);
			break;
		case 5:
			numImg.setImageResource(R.drawable.cinco_btn);
			break;
		case 6:
			numImg.setImageResource(R.drawable.seis_btn);
			break;
		case 7:
			numImg.setImageResource(R.drawable.siete_btn);
			break;
		case 8:
			numImg.setImageResource(R.drawable.ocho_btn);
			break;
		case 9:
			numImg.setImageResource(R.drawable.nueve_btn);
			break;
		}
		numImg.setBackgroundColor(R.color.alpha);
		numImg.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		return numImg;
	}

	private void setNumImg(int num, ImageButton numImgW, TypeButton tb) {

		switch (num) {
		case 1:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.uno_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.uno_w_btn);
			break;
		case 2:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.dos_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.dos_w_btn);
			break;
		case 3:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.tres_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.tres_w_btn);
			break;
		case 4:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.cuatro_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.cuatro_w_btn);
			break;
		case 5:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.cinco_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.cinco_w_btn);
			break;
		case 6:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.seis_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.seis_w_btn);
			break;
		case 7:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.siete_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.siete_w_btn);
			break;
		case 8:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.ocho_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.ocho_w_btn);
			break;
		case 9:
			if (tb.equals(TypeButton.NORMAL))
				numImgW.setImageResource(R.drawable.nueve_btn);
			else if (tb.equals(TypeButton.WARNING))
				numImgW.setImageResource(R.drawable.nueve_w_btn);
			break;
		}
	}
}